#include <cassert>
#include "NeuronLayer.h"

namespace fullsail_ai {

	void NeuronLayer::computeHiddenErrors(std::vector<double> const& arguments,
	                                      std::vector<double> const& outputErrors,
	                                      std::vector<double>& hiddenErrors) const
	{
		assert(
		    (hiddenErrors.size() == arguments.size())
		 && "Your code is violating the 1st precondition of this method!"
		);
		assert(
		    (outputErrors.size() == neurons.size())
		 && "Your code is violating the 2nd precondition of this method!"
		);

		for (std::size_t i = 0; i < hiddenErrors.size(); ++i)
		{
			hiddenErrors[i] = 0.0;
		}

		for (std::size_t j = 0; j < neurons.size(); ++j)
		{
			std::vector<double> const& weights = neurons[j].getWeights();

			for (std::size_t i = 0; i < hiddenErrors.size(); ++i)
			{
				hiddenErrors[i] += outputErrors[j] * weights[i];
			}
		}

		for (std::size_t i = 0; i < hiddenErrors.size(); ++i)
		{
			hiddenErrors[i] *= arguments[i] * (1.0 - arguments[i]);
		}
	}
}  // namespace fullsail_ai

